package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageMoveRecord;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageMoveRecordParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOffShelfParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageMoveRecordResult;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageOffShelfResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageMoveRecordService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.ui.Model;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 移库记录表控制器
 *
 * @author 
 * @Date 2023-09-09 11:20:46
 */
@Controller
@RequestMapping("/hxStorageMoveRecord")
public class HxStorageRecordController extends BaseController {

    private String PREFIX = "/hx/storage/hxStorageMoveRecord";

    @Autowired
    private HxStorageMoveRecordService hxStorageMoveRecordService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicCustomerService basicCustomerService;


    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        //查询可选择客户
        model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/hxStorageMoveRecord.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStorageMoveRecord_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStorageMoveRecord_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxStorageMoveRecordParam hxStorageMoveRecordParam) {
        this.hxStorageMoveRecordService.add(hxStorageMoveRecordParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStorageMoveRecordParam hxStorageMoveRecordParam) {
        this.hxStorageMoveRecordService.update(hxStorageMoveRecordParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStorageMoveRecordParam hxStorageMoveRecordParam) {
        this.hxStorageMoveRecordService.delete(hxStorageMoveRecordParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2023-09-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStorageMoveRecordParam hxStorageMoveRecordParam) {
        HxStorageMoveRecord detail = this.hxStorageMoveRecordService.getById(hxStorageMoveRecordParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2023-09-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStorageMoveRecordParam hxStorageMoveRecordParam,String condition,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageMoveRecordParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageMoveRecordParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        return this.hxStorageMoveRecordService.findPageBySpec(hxStorageMoveRecordParam,user.getOperateId(),condition);
    }

    /**
     * 导出所有
     *
     * @author 
     * @Date 2023-09-09
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<HxStorageMoveRecordResult> listAll(HxStorageMoveRecordParam hxStorageMoveRecordParam,String condition,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageMoveRecordParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageMoveRecordParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        return this.hxStorageMoveRecordService.listAll(hxStorageMoveRecordParam,user.getOperateId(),condition);
    }

    /**
     * 合计行
     *
     * @author 
     * @Date 2023-09-13
     */
    @RequestMapping("/count")
    @ResponseBody
    public ResponseData count(HxStorageMoveRecordParam hxStorageMoveRecordParam,String condition,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageMoveRecordParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageMoveRecordParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        return ResponseData.success(this.hxStorageMoveRecordService.count(hxStorageMoveRecordParam,user.getOperateId(),condition));
    }

    /**
     * 下架人员信息统计
     *
     * @author lww
     * @Date 2023-12-28
     */
    @ResponseBody
    @RequestMapping("/moveUser")
    public LayuiPageInfo moveUser(HxStorageMoveRecordParam hxStorageMoveRecordParam, String condition, String startTime, String endTime) {

        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStorageMoveRecordParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStorageMoveRecordParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        hxStorageMoveRecordParam.setReturnStatus("已完成");
        User user = userService.getcurrentUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        hxStorageMoveRecordParam.setOperateId(user.getOperateId());
        return this.hxStorageMoveRecordService.selectUser(hxStorageMoveRecordParam,condition);
    }

}


